<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Purchase;
use App\Services\PurchaseService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PurchaseController extends Controller
{
    protected $PurchaseService;
    public function __construct(PurchaseService $PurchaseService)
    {
        $this->PurchaseService = $PurchaseService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $name = $request->name;
        $pageSize = $request->pageSize ?: 10;
        $page = $request->page ?: 1;
        $page = $page * $pageSize - $pageSize;

        $sql = $this->PurchaseService->get($name);
        $sql->where('company_id',$request->company_id);
        $sql->where('project_id',$request->project_id);
        $total = $sql->count();
        $data = $sql->skip($page)->take($pageSize)->orderBy('created_at','desc')->get();
        foreach ($data as $k => $v) {
            $data[$k]->approval_status = '';
            if($v->approval){
                $data[$k]->approval_status = $v->approval->status_text;
            }
        }
        return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data]);
    }

    public function all(Request $request)
    {
        $name = $request->name;
        $number = $request->number;
        $sql = $this->PurchaseService->get($name);
        $sql->where('company_id',$request->company_id);
        $sql->where('project_id',$request->project_id);
        $sql->when($number, function ($query, $number) {
            return $query->where(function($query) use ($number) {
                $query->where('number','like', '%'.$number.'%');
            });
        });
        $sql->with('details');
        $total = $sql->count();
        $data = $sql->orderBy('created_at','desc')->get();
        foreach ($data as $k => $v) {
            $data[$k]->delivery_date = strtotime($v->delivery_date) * 1000;
        }
        return response()->json(['code' => 200, 'message' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function show(Request $request)
    {
        $data = Purchase::find($request->id);
        if($data->delivery_date){
            $data->delivery_date = strtotime($data->delivery_date) * 1000;
        }
        $data->details;
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $POSTONLY = Purchase::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['admin_id'] = Auth::user()->id;
        $data = $this->PurchaseService->insert($input,$request->details);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $POSTONLY = Purchase::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['admin_id'] = Auth::user()->id;
        $input['id'] = $request->id;
        $data = $this->PurchaseService->update($input,$request->details);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        if(!$id){
            return response()->json(['code' => 404, 'message' => '请选择要操作的记录']);
        }
        $data = Purchase::find($id);
        $data->delete();
        return response()->json(['code' => 200, 'message' => '操作成功']);
    }
}
